package com.expense.app.admin.controller;

import cn.hutool.log.Log;
import cn.hutool.log.LogFactory;
import com.expense.app.admin.annotation.LoginUser;
import com.expense.app.admin.annotation.OpeLog;
import com.expense.app.admin.model.params.adminUser.AdminUserDetailSaveParam;
import com.expense.app.admin.model.params.adminUser.AdminUserListParam;
import com.expense.app.admin.model.params.adminUser.AdminUserModifyPasswordParam;
import com.expense.app.admin.services.AdminUserServ;
import com.expense.common.entity.params.CheckParam;
import com.expense.common.entity.params.DetailParam;
import com.expense.common.model.auth.AdminUser;
import com.expense.common.util.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.Min;

@Validated
@RestController
@RequestMapping(value = "/adminUser")
public class AdminUserCtrl {
    private static final Log log = LogFactory.get();

    @Autowired
    AdminUserServ adminUserServ;

    @GetMapping("/findAllList")
    public Result findAllList() {
        return Result.success(adminUserServ.findAllEmployeeList());
    }

    @RequestMapping(value = "/findList",method = RequestMethod.GET)
    public Result findList(@Validated AdminUserListParam params) {
        return Result.success(adminUserServ.findList(params));
    }

    @RequestMapping(value = "/findDetail/{id}",method = RequestMethod.GET)
    public Result findDetail (@Validated  DetailParam param) {
        return Result.success(adminUserServ.findDetail(param));
    }


    @RequestMapping(value = "/checkUserName",method = RequestMethod.GET)
    @OpeLog(type = "员工管理",value = "验证登录名是否已存在")
    public Result checkByUsername (@Validated CheckParam checkParam) {
        return Result.success(adminUserServ.checkByUserName(checkParam));
    }


    @RequestMapping(value = "/saveUser",method = RequestMethod.POST)
    @OpeLog(type = "员工管理",value = "保存员工信息")
    public Result saveOne(@Validated @RequestBody AdminUserDetailSaveParam param) {
        return Result.success(adminUserServ.saveOne(param));
    }


    @RequestMapping(value = "/savePassword",method = RequestMethod.POST)
    @OpeLog(type = "员工管理",value = "修改密码")
    public Result savePassword(@Validated @RequestBody AdminUserModifyPasswordParam param, @LoginUser AdminUser adminUser) {
        param.setAdminUser(adminUser);
        return Result.success(adminUserServ.savePassword(param));
    }

    @RequestMapping(value = "/generatePassword",method = RequestMethod.POST)
    public Result generatePassword(@Validated @RequestBody DetailParam param) {
        return Result.success(adminUserServ.generatePassword(param));
    }

    @PutMapping("/resetPassword/{id}")
    public Result generatePassword(@PathVariable @Min(1) Long id) {
        return Result.success(adminUserServ.resetPassword(id));
    }

    @RequestMapping(value = "/delete/{id}",method = RequestMethod.DELETE)
    @OpeLog(type = "员工管理",value = "删除员工")
    public Result deleteOne(@Validated  DetailParam param) {
        return Result.success(adminUserServ.deleteOne(param));
    }

    @RequestMapping(value = "/isEnable",method = RequestMethod.PUT)
    @OpeLog(type = "员工管理",value = "禁用启用员工")
    public Result isEnableOne(@Validated  @RequestBody DetailParam param) {
        return Result.success(adminUserServ.isEnableOne(param));
    }

}
